package com.dev.utils.app.helper;


import com.dev.utils.LogUtils;
import com.dev.utils.app.*;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;

import java.lang.ref.WeakReference;

import static com.dev.utils.app.HandlerUtils.getMainHandler;


/**
 * detail: 简化链式设置 View Helper 类
 *
 * @author Ttt
 * <pre>
 *     整合 {@link ViewHelper}、{@link DevHelper} 部分代码
 *     针对单个 View 设置处理, 无需多次传入 View
 * </pre>
 */
public class QuickHelper {
    // 日志 TAG
    private static final String TAG = QuickHelper.class.getSimpleName();

    // 持有 View
    private final WeakReference<Component> mViewRef;

    /**
     * 构造函数
     *
     * @param target 目标 View
     */
    public QuickHelper(final Component target) {
        this.mViewRef = new WeakReference<>(target);
    }

    /**
     * 获取 QuickHelper
     *
     * @param target 目标 View
     * @return {@link QuickHelper}
     */
    public static QuickHelper get(final Component target) {
        return new QuickHelper(target);
    }

    /**
     * 获取 View
     *
     * @param <T> 泛型
     * @return {@link Component}
     */
    public <T extends Component> T getView() {
        return ViewUtils.convertView(targetView());
    }

    /**
     * 获取目标 View
     *
     * @return {@link Component}
     */
    private Component targetView() {
        return mViewRef.get();
    }

    /**
     * 获取目标 View ( 转 ViewGroup )
     *
     * @return {@link ComponentContainer}
     */
    private ComponentContainer targetViewGroup() {
        try {
            return (ComponentContainer) targetView();
        } catch (Exception e) {
            LogUtils.error(TAG, e + "targetViewGroup");
        }
        return null;
    }

    /**
     * 设置 View LayoutParams
     *
     * @return {@link QuickHelper}
     */
    public QuickHelper setLayoutParams(final ComponentContainer.LayoutConfig params) {
        ViewUtils.setLayoutParams(targetView(), params);
        return this;
    }

    /**
     * 设置背景图片
     *
     * @param background 背景图片
     * @return {@link QuickHelper}
     */
    public QuickHelper setBackground(final Element background) {
        ViewUtils.setBackground(targetView(), background);
        return this;
    }

    /**
     * 设置背景图片
     *
     * @param view       {@link Component}
     * @param background 背景图片
     * @return {@link Component}
     */
    public static Component setBackground(
            final Component view,
            final Element background
    ) {
        if (view != null) {
            try {
                view.setBackground(background);
            } catch (Exception e) {
            }
        }
        return view;
    }

    /**
     * 设置最大行数
     *
     * @param maxLines 最大行数
     * @return {@link QuickHelper}
     */
    public QuickHelper setMaxLines(final int maxLines) {
        TextViewUtils.setMaxLines(targetTextView(), maxLines);
        return this;
    }


    /**
     * 获取目标 View ( 转 ImageView )
     *
     * @return {@link Image}
     */
    private Image targetImageView() {
        try {
            return (Image) targetView();
        } catch (Exception e) {
            LogUtils.error(TAG, e + "targetImageView");
        }
        return null;
    }

    /**
     * 获取目标 View ( 转 TextView )
     *
     * @return {@link Text}
     */
    private Text targetTextView() {
        try {
            return (Text) targetView();
        } catch (Exception e) {
            LogUtils.error(TAG, e + "targetTextView");
        }
        return null;
    }

    /**
     * 获取目标 View ( 转 EditText )
     *
     * @return {@link TextField}
     */
    private TextField targetEditText() {
        try {
            return (TextField) targetView();
        } catch (Exception e) {
            LogUtils.error(TAG, e + "targetEditText");
        }
        return null;
    }

    // ==========
    // = Helper =
    // ==========

    /**
     * 获取 ViewHelper
     *
     * @return {@link ViewHelper}
     */
    public ViewHelper viewHelper() {
        return ViewHelper.get();
    }

    /**
     * 获取 DevHelper
     *
     * @return {@link DevHelper}
     */
    public DevHelper devHelper() {
        return DevHelper.get();
    }

    /**
     * 获取 QuickHelper
     *
     * @param target 目标 View
     * @return {@link QuickHelper}
     */
    public QuickHelper quickHelper(final Component target) {
        return QuickHelper.get(target);
    }


    // ==============
    // = ViewHelper =
    // ==============

    // ===========
    // = Handler =
    // ===========

    /**
     * 在主线程 Handler 中执行任务
     *
     * @param runnable 可执行的任务
     * @return {@link QuickHelper}
     */
    public QuickHelper postRunnable(final Runnable runnable) {
        HandlerUtils.postRunnable(runnable);
        return this;
    }

    /**
     * 在主线程 Handler 中执行延迟任务
     *
     * @param runnable    可执行的任务
     * @param delayMillis 延迟时间
     */
    public static void postRunnable(
            final Runnable runnable,
            final long delayMillis
    ) {
        if (runnable != null) {
            getMainHandler().postTask(runnable, delayMillis);
        }
    }

    /**
     * 在主线程 Handler 中执行延迟任务
     *
     * @param runnable    可执行的任务
     * @param delayMillis 延迟时间
     * @param number      轮询次数
     * @param interval    轮询时间
     * @return {@link QuickHelper}
     */
    public QuickHelper postRunnable(
            final Runnable runnable,
            final long delayMillis,
            final int number,
            final int interval
    ) {
        HandlerUtils.postRunnable(runnable, delayMillis, number, interval);
        return this;
    }

    /**
     * 在主线程 Handler 中清除任务
     *
     * @param runnable 需要清除的任务
     * @return {@link QuickHelper}
     */
    public QuickHelper removeRunnable(final Runnable runnable) {
        HandlerUtils.removeRunnable(runnable);
        return this;
    }

    // ========
    // = Text =
    // ========

    /**
     * 设置 Hint 文本
     *
     * @param text Hint text
     * @return {@link QuickHelper}
     */
    public QuickHelper setHint(final CharSequence text) {
        TextViewUtils.setHint(targetTextView(), text);
        return this;
    }


    /**
     * 设置文本
     *
     * @param text TextView text
     * @return {@link QuickHelper}
     */
    public QuickHelper setText(final CharSequence text) {
        Component view = targetView();
        if (view instanceof TextField) {
            EditTextUtils.setText(EditTextUtils.getEditText(view), text);
        } else {
            TextViewUtils.setText(view, text);
        }
        return this;
    }

    /**
     * 设置字体颜色
     *
     * @param color R.color.id
     * @return {@link QuickHelper}
     */
    public QuickHelper setTextColor(final int color) {
        TextViewUtils.setTextColor(targetTextView(), color);
        return this;
    }

    public QuickHelper setTextSize(final int size) {
        TextViewUtils.setTextSize(targetTextView(), Text.TextSizeType.PX, size);
        return this;
    }


    /****************************************************
     *
     * 目前使用的方法添加到后面
     *
     *******************************************************/
    /**
     * 设置 Padding 边距
     *
     * @param padding Padding
     * @return {@link QuickHelper}
     */
    public QuickHelper setPadding(final int padding) {
        ViewUtils.setPadding(targetView(), padding);
        return this;
    }

    /**
     * 设置 View Bottom Margin
     *
     * @param bottomMargin Bottom Margin
     * @return {@link QuickHelper}
     */
    public QuickHelper setMarginBottom(final int bottomMargin) {
        ViewUtils.setMarginBottom(targetView(), bottomMargin);
        return this;
    }


    /**
     * 设置 Padding 边距
     *
     * @param left   Left Padding
     * @param top    Top Padding
     * @param right  Right Padding
     * @param bottom Bottom Padding
     * @return {@link QuickHelper}
     */
    public QuickHelper setPadding(
            final int left,
            final int top,
            final int right,
            final int bottom
    ) {
        ViewUtils.setPadding(targetView(), left, top, right, bottom);
        return this;
    }

    // ============
    // = Listener =
    // ============

    /**
     * 设置点击事件
     *
     * @param onClickListener {@link Component.ClickedListener}
     * @return {@link QuickHelper}
     */
    public QuickHelper setOnClicks(final Component.ClickedListener onClickListener) {
        ListenerUtils.setOnClicks(onClickListener, targetView());
        return this;
    }

    /**
     * 设置 TextView 是否加粗
     *
     * @return {@link QuickHelper}
     */
    public QuickHelper setBold() {
        TextViewUtils.setBold(targetView());
        return this;
    }


    /**
     * 设置 TextView 是否加粗
     *
     * @param isBold {@code true} yes, {@code false} no
     * @return {@link QuickHelper}
     */
    public QuickHelper setBold(final boolean isBold) {
        TextViewUtils.setBold(targetTextView(), isBold);
        return this;
    }

    /**
     * 设置 View Left Padding
     *
     * @param leftPadding Left Padding
     * @return {@link QuickHelper}
     */
    public QuickHelper setPaddingLeft(final int leftPadding) {
        ViewUtils.setPaddingLeft(targetView(), leftPadding);
        return this;
    }

    /**
     * 设置 View Top Padding
     *
     * @param topPadding Top Padding
     * @return {@link QuickHelper}
     */
    public QuickHelper setPaddingTop(final int topPadding) {
        ViewUtils.setPaddingTop(targetView(), topPadding);
        return this;
    }

    /**
     * 设置 View Top Padding
     *
     * @param topPadding Top Padding
     * @param reset      是否重置清空其他 Padding
     * @return {@link QuickHelper}
     */
    public QuickHelper setPaddingTop(
            final int topPadding,
            final boolean reset
    ) {
        ViewUtils.setPaddingTop(targetView(), topPadding, reset);
        return this;
    }


    /**
     * 设置 View Right Padding
     *
     * @param rightPadding Right Padding
     * @return {@link QuickHelper}
     */
    public QuickHelper setPaddingRight(final int rightPadding) {
        ViewUtils.setPaddingRight(targetView(), rightPadding);
        return this;
    }

    /**
     * 设置 View 显示的状态
     *
     * @param isVisibility {@code true} View.VISIBLE, {@code false} View.GONE
     * @return {@link QuickHelper}
     */
    public QuickHelper setVisibility(final boolean isVisibility) {
        ViewUtils.setVisibility(isVisibility, targetView());
        return this;
    }

    /**
     * 设置 View Top Margin
     *
     * @param view      {@link Component}
     * @param topMargin Top Margin
     * @return {@code true} success, {@code false} fail
     */
    public static boolean setMarginTop(
            final Component view,
            final int topMargin
    ) {
        return setMarginTop(view, topMargin, true);
    }

    /**
     * 设置 View Top Margin
     *
     * @param view      {@link Component}
     * @param topMargin Top Margin
     * @param reset     是否重置清空其他 margin
     * @return {@code true} success, {@code false} fail
     */
    public static boolean setMarginTop(
            final Component view,
            final int topMargin,
            final boolean reset
    ) {
        if (reset) return setMargin(view, 0, topMargin, 0, 0);
        int[] margin = getMargin(view);
        return setMargin(view, margin[0], topMargin, margin[2], margin[3]);
    }

    /**
     * 设置 Margin 边距
     *
     * @param view   {@link Component}
     * @param left   Left Margin
     * @param top    Top Margin
     * @param right  Right Margin
     * @param bottom Bottom Margin
     * @return {@code true} success, {@code false} fail
     */
    public static boolean setMargin(
            final Component view,
            final int left,
            final int top,
            final int right,
            final int bottom
    ) {
        if (view != null && view.getLayoutConfig() != null) {
            if (view.getLayoutConfig() instanceof ComponentContainer.LayoutConfig) {
                ComponentContainer.LayoutConfig layoutParams = (ComponentContainer.LayoutConfig) view.getLayoutConfig();
                layoutParams.setMargins(left, top, right, bottom);
                view.setLayoutConfig(layoutParams);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取 View Margin
     *
     * @param view {@link Component}
     * @return new int[] {left, top, right, bottom}
     */
    public static int[] getMargin(final Component view) {
        int[] margin = new int[]{0, 0, 0, 0};
        if (view != null && view.getLayoutConfig() != null) {
            if (view.getLayoutConfig() instanceof ComponentContainer.LayoutConfig) {
                ComponentContainer.LayoutConfig layoutParams = (ComponentContainer.LayoutConfig) view.getLayoutConfig();
                margin[0] = layoutParams.getMarginLeft();
                margin[1] = layoutParams.getMarginTop();
                margin[2] = layoutParams.getMarginRight();
                margin[3] = layoutParams.getMarginBottom();
            }
        }
        return margin;
    }

    /**
     * 设置 Component Left Margin
     *
     * @param view       {@link Component}
     * @param leftMargin Left Margin
     * @return {@code true} success, {@code false} fail
     */
    public static boolean setMarginLeft(
            final Component view,
            final int leftMargin
    ) {
        return setMarginLeft(view, leftMargin, true);
    }

    /**
     * 设置 Component Left Margin
     *
     * @param view       {@link Component}
     * @param leftMargin Left Margin
     * @param reset      是否重置清空其他 margin
     * @return {@code true} success, {@code false} fail
     */
    public static boolean setMarginLeft(
            final Component view,
            final int leftMargin,
            final boolean reset
    ) {
        if (reset) return setMargin(view, leftMargin, 0, 0, 0);
        int[] margin = getMargin(view);
        return setMargin(view, leftMargin, margin[1], margin[2], margin[3]);
    }


    /**
     * 设置字体大小 ( sp 缩放像素 )
     *
     * @param size 字体大小
     * @return {@link QuickHelper}
     */
    public QuickHelper setTextSizeBySp(final float size) {
        TextViewUtils.setTextSizeByVp(targetTextView(), (int) size);
        return this;
    }


    /**
     * 设置 View Top Margin
     *
     * @param topMargin Top Margin
     * @param reset     是否重置清空其他 margin
     * @return {@link QuickHelper}
     */
    public QuickHelper setMarginTop(
            final int topMargin,
            final boolean reset
    ) {
        ViewUtils.setMarginTop(targetView(), topMargin, reset);
        return this;
    }
    /**
     * 设置 View Top Margin
     *
     * @param topMargin Top Margin
     * @return {@link QuickHelper}
     */
    public QuickHelper setMarginTop(final  int topMargin){
        ViewUtils.setMarginTop(topMargin,targetView());
        return this;
    }
    /**
     * 设置 View Top Margin
     *
     * @param leftMargin Top Margin
     * @return {@link QuickHelper}
     */
    public QuickHelper setMarginLeft(final  int leftMargin){
        ViewUtils.setMarginLeft(leftMargin,targetView());
        return this;
    }
}
